import requests
import time
import socket

# 检查端口是否开放
def check_port(host='127.0.0.1', port=5001):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(2)
    result = sock.connect_ex((host, port))
    sock.close()
    return result == 0

# 发送简单的GET请求
def test_server():
    print(f"检查端口 5001 是否开放...")
    port_open = check_port()
    print(f"端口状态: {'开放' if port_open else '关闭'}")
    
    if port_open:
        print("尝试发送GET请求到 http://127.0.0.1:5001")
        try:
            response = requests.get("http://127.0.0.1:5001", timeout=5)
            print(f"响应状态码: {response.status_code}")
            print(f"响应内容: {response.text[:100]}...")
        except requests.exceptions.RequestException as e:
            print(f"请求失败: {e}")
    else:
        print("端口未开放，服务可能未启动或启动失败")

if __name__ == "__main__":
    print("开始检查服务器状态...")
    time.sleep(1)  # 短暂等待确保服务有时间启动
    test_server()